package com.dsbj.apolo.facecollector.utils;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.*;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class ExcelUtils {

    public static List<List<Object>> getExcelData(InputStream inputStream,int startRow,int startCell){
        List<List<Object>> dataList = new ArrayList<>();
        Workbook workbook = null;
        try {
            workbook = WorkbookFactory.create(inputStream);
            inputStream.close();
            Sheet sheet = workbook.getSheetAt(0);
            int rowLength = sheet.getLastRowNum() -1;
            Row row = sheet.getRow(0);
            //总列数
            int colLength = row.getLastCellNum();
            Cell cell = row.getCell(0);
            for (int i = startRow; i <= rowLength; i++) {
                row = sheet.getRow(i);
                List<Object> rowData = new ArrayList<>();
                for (int j = startCell; j <= colLength; j++) {
                    cell = row.getCell(j);
                    String cellData = "";
                    if (cell != null) {
                        cell.setCellType(Cell.CELL_TYPE_STRING);
                        String data = cell.getStringCellValue();
                        cellData = data.trim();
                    }
                    rowData.add(cellData);
                }
                if(!StringUtils.isBlank((String) rowData.get(0))){
                    dataList.add(rowData);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidFormatException e) {
            e.printStackTrace();
        }


        return dataList;

    }
}
